home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / golded / tools / gedapp / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-26  |  8.4 KB  |  409 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.   GEDApp v1.0 - GoldED AppIcon handler, ©1995 Dietmar Eilert
  4.  
  5.   DICE:
  6.  
  7.   dcc main.c appIconA.a -// -proto -mRR -mi -pr -2.0 -o ram:GEDApp
  8.  
  9.   ------------------------------------------------------------------------------
  10. */
  11.  
  12. /// "includes"
  13.  
  14. #include <amiga20/exec/exec.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20. #include <amiga20/intuition/intuition.h>
  21. #include <amiga20/dos/dos.h>
  22. #include <amiga20/dos/dosextens.h>
  23. #include <amiga20/dos/rdargs.h>
  24. #include <amiga20/dos/dostags.h>
  25. #include <amiga20/workbench/startup.h>
  26. #include <amiga20/workbench/workbench.h>
  27. #include <amiga20/rexx/errors.h>
  28. #include <amiga20/rexx/rxslib.h>
  29.  
  30. #include <amiga20/clib/alib_protos.h>
  31. #include <amiga20/clib/dos_protos.h>
  32. #include <amiga20/clib/exec_protos.h>
  33. #include <amiga20/clib/icon_protos.h>
  34. #include <amiga20/clib/intuition_protos.h>
  35. #include <amiga20/clib/utility_protos.h>
  36. #include <amiga20/clib/rexxsyslib_protos.h>
  37. #include <amiga20/clib/wb_protos.h>
  38.  
  39. #ifdef PRAGMAS
  40.  
  41. #include "Pragmas/exec.h"
  42. #include "Pragmas/disk.h"
  43. #include "Pragmas/diskfont.h"
  44. #include "Pragmas/dynamic.h"
  45. #include "Pragmas/gadtools.h"
  46. #include "Pragmas/keymap.h"
  47. #include "Pragmas/graphics.h"
  48. #include "Pragmas/icon.h"
  49. #include "Pragmas/input.h"
  50. #include "Pragmas/intuition.h"
  51. #include "Pragmas/layers.h"
  52. #include "Pragmas/locale.h"
  53. #include "Pragmas/misc.h"
  54. #include "Pragmas/timer.h"
  55. #include "Pragmas/wb.h"
  56. #include "Pragmas/xpkmaster.h"
  57. #include "Pragmas/amigaguide.h"
  58. #include "Pragmas/reqtools.h"
  59.  
  60. #endif
  61.  
  62. #define Prototype    extern
  63. #define MAX_LEN      150
  64.  
  65. ///
  66. /// "prototypes"
  67.  
  68. Prototype void   main(ULONG, char **);
  69. Prototype int    wbmain(struct WBStartup *);
  70. Prototype void   MainLoop(void);
  71. Prototype char  *MakeFileName(char *, char *);
  72. Prototype char  *CompletePath(char *);
  73. Prototype char  *StartGED(void);
  74. Prototype struct RexxMsg *SendRexxCommand(char *, char *, struct MsgPort *);
  75. Prototype void   FreeRexxCommand (struct RexxMsg *);
  76. Prototype ULONG  WaitForAnswer(struct MsgPort *);
  77. Prototype char  *LookForGED(void);
  78. Prototype void   ReadWBCmd(ULONG, struct WBArg *);
  79.  
  80. extern struct Library *IconBase;
  81. extern struct Library *DOSBase;
  82. extern struct Library *SysBase;
  83. extern struct Library *IntuitionBase;
  84. extern struct Library *WorkbenchBase;
  85.  
  86. ///
  87. /// "entry points"
  88.  
  89. void
  90. main(argc, argv)
  91.  
  92. ULONG argc;
  93. char *argv[];
  94. {
  95.     MainLoop();
  96. }
  97.  
  98. int
  99. wbmain(struct WBStartup *wbs)
  100. {
  101.     MainLoop();
  102. }
  103.  
  104.  
  105. ///
  106. /// "main loop"
  107.  
  108. /* --------------------------------- MainLoop ----------------------------------
  109.  
  110.  Open AppIcon, handle incoming messages
  111.  
  112. */
  113.  
  114. void
  115. MainLoop()
  116. {
  117.     const char *version = "$VER: GEDApp 1.0 (26.1.95)";
  118.  
  119.     struct DiskObject *appDiskObject;
  120.  
  121.     if (!(appDiskObject = GetDiskObject("golded:config/AppIcon")))
  122.         appDiskObject = GetDefDiskObject(WBTOOL);
  123.  
  124.     if (appDiskObject) {
  125.  
  126.         struct MsgPort     *msgPort;
  127.         struct AppMessage  *amsg;
  128.         struct AppIcon     *appIcon;
  129.  
  130.         if (msgPort = CreateMsgPort()) {
  131.  
  132.             if (appIcon = AddAppIconA(0, NULL, "GED", msgPort, NULL, appDiskObject, TAG_END)) {
  133.  
  134.                 BOOL terminated = FALSE;
  135.  
  136.                 while (!terminated) {
  137.  
  138.                     WaitPort(msgPort);
  139.  
  140.                     while (amsg = (struct AppMessage *)GetMsg(msgPort)) {
  141.  
  142.                         if (amsg->am_NumArgs)
  143.                             ReadWBCmd(amsg->am_NumArgs, amsg->am_ArgList);
  144.                         else
  145.                             terminated = TRUE;
  146.  
  147.                         ReplyMsg((struct Message *)amsg);
  148.                     }
  149.                 }
  150.             }
  151.             else
  152.                 puts("Couldn't allocate AppIcon. Workbench closed ?!");
  153.  
  154.             RemoveAppIcon(appIcon);
  155.  
  156.             DeleteMsgPort(msgPort);
  157.         }
  158.         else
  159.             puts("Couldn't create message port ?!");
  160.  
  161.         FreeDiskObject(appDiskObject);
  162.     }
  163.     else
  164.         puts("Impossible d'allouer DiskObject ?!");
  165.  
  166.     exit(0);
  167. }
  168.  
  169.  
  170. ///
  171. /// "misc"
  172.  
  173. /* ------------------------------- MakeFileName --------------------------------
  174.  
  175.  Build fully qualified path from file/path names; return pointer to static copy.
  176.  
  177.  Générer le chemin d'accès complet à partir des icônes; retourne un pointeur
  178.  pour une copie statique.
  179.  
  180. */
  181.  
  182. char *
  183. MakeFileName(path, file)
  184.  
  185. char *path, *file;
  186. {
  187.     static char buffer[MAX_LEN + 1];
  188.  
  189.     strcpy(buffer, "\42");
  190.  
  191.     strcat(buffer, path);
  192.  
  193.     CompletePath(buffer);
  194.  
  195.     strcat(buffer, file);
  196.  
  197.     strcat(buffer, "\42");
  198.  
  199.     return(buffer);
  200. }
  201.  
  202. /* ------------------------------ CompletePath -----------------------------------
  203.  
  204.  Add '/' to path if missing so far
  205.  
  206. */
  207.  
  208. char *
  209. CompletePath(char *path)
  210. {
  211.     UWORD len;
  212.  
  213.     if (len = strlen(path))
  214.         if ((path[len - 1] != ':') && (path[len - 1] != '/'))
  215.             strcat(path, "/");
  216.  
  217.     return(path);
  218. }
  219.  
  220. /* ---------------------------------- ReadWBCmd --------------------------------
  221.  
  222.  Parse AppIcon message
  223.  
  224. */
  225.  
  226. void
  227. ReadWBCmd(numArgs, argList)
  228.  
  229. ULONG  numArgs;
  230. struct WBArg  *argList;
  231. {
  232.     char *host;
  233.     BOOL loadGED = !(host = LookForGED());
  234.  
  235.     if (loadGED)
  236.         host = StartGED();
  237.  
  238.     if (host) {
  239.  
  240.         struct MsgPort *replyPort;
  241.  
  242.         if (replyPort = CreateMsgPort()) {
  243.  
  244.             if (SendRexxCommand(host, "LOCK CURRENT", replyPort)) {
  245.  
  246.                 if (WaitForAnswer(replyPort) == RC_OK) {
  247.  
  248.                     UWORD count;
  249.                     char  path[MAX_LEN + 1], *command;
  250.  
  251.                     for (count = 0; numArgs--; count++) {
  252.  
  253.                         NameFromLock(argList[count].wa_Lock, path, MAX_LEN);
  254.  
  255.                         command = MakeFileName(path, argList[count].wa_Name);
  256.  
  257.                         strins(command, "OPEN SMART QUIET ");
  258.  
  259.                         if (SendRexxCommand(host, command, replyPort))
  260.  
  261.                             WaitForAnswer(replyPort);
  262.                     }
  263.                 }
  264.  
  265.                 if (SendRexxCommand(host, "UNLOCK", replyPort))
  266.  
  267.                     WaitForAnswer(replyPort);
  268.             }
  269.  
  270.             DeleteMsgPort(replyPort);
  271.         }
  272.     }
  273. }
  274.  
  275.  
  276. /* ----------------------------------- LookForGED ----------------------------
  277.  
  278.  Look for running GoldED task
  279.  
  280. */
  281.  
  282. char *
  283. LookForGED()
  284. {
  285.     static char host[] = "GOLDED.1";
  286.     UWORD  try;
  287.  
  288.     for (try = '1'; try <= '9'; try++) {
  289.  
  290.         host[7] = try;
  291.  
  292.         if (FindPort(host))
  293.             return(host);
  294.     } 
  295.  
  296.     return(NULL);
  297. }
  298.  
  299. /* ------------------------------------- StartGED -----------------------------
  300.  
  301.  Launch a new GoldED task. Return pointer to host name (or NULL).
  302.  
  303. */
  304.  
  305. char *
  306. StartGED()
  307. {
  308.     static char *host = "GOLDED.1";
  309.  
  310.     if (!SystemTags("GoldED:GoldED", SYS_Asynch, TRUE, SYS_Input, NULL, SYS_Output, NULL, TAG_DONE))
  311.  
  312.         UWORD try;
  313.  
  314.         for (try = 50; try; try--, Delay(10))
  315.             if (FindPort(host))
  316.                 return(host);
  317.  
  318.     return(FALSE);
  319. }
  320.  
  321. ///
  322. /// "ARexx"
  323.  
  324. /* -------------------------------------- WaitForAnswer -----------------------
  325.  
  326.   Wait for answer on previously sent message. Free message afterwards. Primary
  327.   return code is returned.
  328.  
  329. */
  330.  
  331. ULONG
  332. WaitForAnswer(port)
  333.  
  334. struct MsgPort *port;
  335. {
  336.     struct RexxMsg *rexxMsg;
  337.     ULONG  result;
  338.  
  339.     do {
  340.         
  341.         WaitPort(port);
  342.  
  343.         if (rexxMsg = (struct RexxMsg *)GetMsg(port))
  344.             result = rexxMsg->rm_Result1;
  345.  
  346.     } while (!rexxMsg);
  347.  
  348.     FreeRexxCommand(rexxMsg);
  349.  
  350.     return(result);
  351. }
  352.  
  353. /* ------------------------------------- FreeRexxCommand ----------------------
  354.  
  355.  Free ARexx message
  356.  
  357. */
  358.  
  359. void
  360. FreeRexxCommand(rexxmessage)
  361.  
  362. struct RexxMsg *rexxmessage;
  363. {
  364.     if (rexxmessage->rm_Result1 == RC_OK) 
  365.         if (rexxmessage->rm_Result2)
  366.             DeleteArgstring((char *)rexxmessage->rm_Result2);
  367.  
  368.     DeleteArgstring((char *)ARG0(rexxmessage));
  369.  
  370.     DeleteRexxMsg(rexxmessage);
  371. }
  372.  
  373. /* ---------------------------------- SendRexxCommand -------------------------
  374.  
  375.  Send ARexx message
  376.  
  377. */
  378.  
  379. struct RexxMsg *
  380. SendRexxCommand(port, cmd, replyPort)
  381.  
  382. char   *cmd,   *port;
  383. struct MsgPort *replyPort;
  384. {
  385.     struct MsgPort *rexxport;
  386.     struct RexxMsg *rexx_command_message = NULL;
  387.  
  388.     Forbid();
  389.  
  390.     if (rexxport = FindPort(port)) {
  391.  
  392.         if (rexx_command_message = CreateRexxMsg(replyPort, NULL, NULL)) {
  393.  
  394.             if (rexx_command_message->rm_Args[0] = CreateArgstring(cmd, strlen(cmd))) {
  395.  
  396.                 rexx_command_message->rm_Action = RXCOMM | RXFF_RESULT;
  397.  
  398.                 PutMsg(rexxport, &rexx_command_message->rm_Node);
  399.             }
  400.         }
  401.     }
  402.  
  403.     Permit();
  404.  
  405.     return(rexx_command_message);
  406. }
  407.  
  408. ///
  409.